# '''
# 将pth 模型转化pt模型（采用onnx）
# '''
import torch
import numpy as np
import pandas as pd
import os
# import torchvision
# from torchvision import transforms
import io
import sys
import torch.nn as nn
import torch.utils.data
import time
import math
import torch.nn.functional as F
# import pointnet_utils
# from pointnet_utils import PointNetEncoder, feature_transform_reguliarzer
#from data_utils.ModelNetDataLoader import ModelNetDataLoader
#import argparse
start_time=time.time()

####数据处理
#
def pc_normalize(pc):
    centroid = np.mean(pc, axis=0)
    pc = pc - centroid
    m = np.max(np.sqrt(np.sum(pc**2, axis=1)))
    pc = pc / m
    return pc
# ###加载.pt文件
def recognize(datapath,modelpath):

    with open(modelpath, 'rb') as f:
        buffer = io.BytesIO(f.read())

    model1=torch.jit.load(buffer)
    ####读取数据
    pointNum=1024
    datalength = 1200
    DATA=pd.read_csv(datapath,header=None,index_col=None)
    data11=DATA.values[:,1:3]  ##读取数据的第2、3位数据
    data22=data11
    endFinal=-1

    data33=[]
    data44=[]
    data55=[]
    if (data22.shape[0]<5):
        print('手势数据小于5，判定为无效数据')
        endFinal=-1
    else:
        data22[:,0]=np.cumsum(data11[:,0], axis=0)
        data22[:,1]=np.cumsum(data11[:,1], axis=0)
        data22=pc_normalize(data22)
        dataz=np.linspace(0,data22.shape[0]-1,data22.shape[0])
        dataz=(dataz-min(dataz))/(max(dataz)-min(dataz))
        dataz=dataz.T
        print(data22.shape,dataz.shape,type(dataz))
        data33=np.column_stack((data22,dataz))
        print('$$$$$',data33)
        data33[:,0]=-data33[:,0]
        data33[:, 1] = -data33[:, 1]
        data33[:, 2] = data33[:, 2]
        times = int(np.ceil(datalength / data33.shape[0]))  ##增加的倍数
        for U in range(times):
            data55.append(data33)
        data3 = np.array(data55)
        data4 = data3.reshape((-1, 3))
        data5 = data4[0:1024, :]
        print(data5.shape)
        # ##数据格式转化
        var=torch.tensor(data5)
        var1=var.transpose(1,0)
        var2=var1.unsqueeze(0)
        var3=var2.type(torch.float32)
        # # ###进行预测

        traced_script_module,_ =model1(var3)
        finalindex=traced_script_module.data.max(1)[1].numpy()
        softMaxScore=math.exp(traced_script_module.data.max(1)[0].numpy())
        print("相似度:",softMaxScore)
        if (softMaxScore > 0.9):
            endFinal=finalindex
            ###tensor 转化数组
            if (endFinal == 0):
                endFinal = 1
            elif (endFinal == 1):
                endFinal = 2
            elif (endFinal == 2):
                # zhengniflag=zhengNiCircle(datar)
                # if (zhengniflag<0):
                #     endFinal = 8
                # else:
                #     endFinal=zhengniflag
                endFinal = 8
            elif (endFinal == 3):
                # zhengniflag = zhengNiCircle(datar)
                # if (zhengniflag < 0):
                #     endFinal = 9
                # else:
                #     endFinal = zhengniflag
                endFinal = 9
            elif (endFinal == 4):
                endFinal = 10
            else:
                endFinal = 11
        else:

            endFinal = -1
    return endFinal

def entry(datapath,modelpath):
    # modelpath = r'D:\zhanbo\蓝牙手势识别\Pointnet_Pointnet2_pytorch\save.pt'
    # datapath = R'E:\卢国璇慢\11\11_004479 (1).txt'
    out=recognize(datapath, modelpath)
    print(out)
    return out





